<?php

/**
 * Project: CrazyPoems
 * Author: Alexandru
 * Date: 20/10/2008
 * File: new-member.php
 */

// Load default settings.
require('inc/config.php');
require('inc/functions.misc.php');
require('inc/functions.index.php');
require('inc/functions.register.php');
include_once 'inc/securimage/securimage.php';
$securimage = new Securimage();

// Redirect if already registered.
if (isset($cookie->username) && strlen($cookie->username) > 0) {
  header("Location: ".BASE_URL);
}

$smarty->assign('pageTitle', l('Cuenta nueva'));
$smarty->assign('breadcrumbs', '<a href="'.BASE_URL.'">'.l('Inicio').'</a> &raquo;' .l('Cuenta nueva'));

// Define variables.
$error_msg = "";
$username = isset($_POST['username']) && strlen($_POST['username']) > 0 ? $_POST['username'] : '';
$fullname = isset($_POST['fullname']) && strlen($_POST['fullname']) > 0 ? $_POST['fullname'] : '';
$email = isset($_POST['email']) && strlen($_POST['email']) > 0 ? $_POST['email'] : '';
$email2 = isset($_POST['email2']) && strlen($_POST['email2']) > 0 ? $_POST['email2'] : '';
$password = isset($_POST['password']) && strlen($_POST['password']) > 0 ? $_POST['password'] : '';
$password2 = isset($_POST['password2']) && strlen($_POST['password2']) > 0 ? $_POST['password2'] : '';
$agreement = isset($_POST['agreement']) && strlen($_POST['agreement']) > 0 ? $_POST['agreement'] : 'no';

// Validate data.
if (isset($_POST['form_sent']) && $_POST['form_sent'] == "yes" && !REGISTRATION_DISABLED) {
  validateUserName($username);
  if (strlen($fullname) > 2) {
    $fullname = substr($fullname,0,50); // Cut to 50
  } else {
    $error_msg .= "<p>" .l('Escriba su nombre.')."</p>";
  }
  validateEmailAddress();
  if (strlen($password) > 0) {
    if ($password != $password2) {
      $error_msg .= "<p>".l('Las contraseñas deben ser idénticas.')."</p>";
    }
  } else {
    $error_msg .= "<p>".l('Introducir Contraseña')."</p>";
  }
  if ($securimage->check($_POST['captcha_code']) == false) {
   $error_msg .= "<p>".l('El código de verificación introducido es incorrecto.')."</p>";
  }
  if ($agreement == 'no') {
    $error_msg .= "<p>".l('Es necesario estar de acuerdo con las')." <strong>".l('Condiciones de uso')."</strong>.</p>";
  }
  // Add user.
  if (strlen($error_msg) == 0) {
    Hop_newUser($username, $fullname, $email, $password);
  }
}

// Smarty.
$smarty->assign('form_sent', $_POST['form_sent']);
$smarty->assign('next_step', $_REQUEST['next_step']);
$smarty->assign('error_msg', $error_msg);
$smarty->assign('username', $username);
$smarty->assign('fullname', $fullname);
$smarty->assign('email', $email);
$smarty->assign('email2', $email2);
$smarty->assign('agreement', $agreement);
$smarty->assign('REGISTRATION_DISABLED', REGISTRATION_DISABLED);

// Go!
$smarty->display('new-member.tpl');

// End page.
require('inc/end.php');
?>
